package com.ruoyi.web.controller.equipment;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.equipment.EquipmentService;
import com.ruoyi.system.domain.equipment.EquipmentServiceDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IEquipmentServiceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备保养项目Controller
 *
 * @author ruoyi
 * @date 2023-04-13
 */
@Api(tags = "设备保养项目api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/equipment/service" )
public class EquipmentServiceController extends BaseController {

    private final IEquipmentServiceService iEquipmentServiceService;

    /**
     * 查询设备保养项目列表
     */
    @ApiOperation(value = "查询设备保养项目列表",response = EquipmentServiceDTO.class)
    @GetMapping("/list")
    public TableDataInfo list(EquipmentService equipmentService)
    {
        startPage();
        LambdaQueryWrapper<EquipmentService> lqw = new LambdaQueryWrapper<EquipmentService>();
        if (StringUtils.isNotBlank(equipmentService.getItemCode())){
            lqw.eq(EquipmentService::getItemCode ,equipmentService.getItemCode());
        }
        if (StringUtils.isNotBlank(equipmentService.getItemName())){
            lqw.like(EquipmentService::getItemName ,equipmentService.getItemName());
        }
        if (StringUtils.isNotBlank(equipmentService.getKeepDetailed())){
            lqw.eq(EquipmentService::getKeepDetailed ,equipmentService.getKeepDetailed());
        }
        if (StringUtils.isNotBlank(equipmentService.getWorkingHours())){
            lqw.eq(EquipmentService::getWorkingHours ,equipmentService.getWorkingHours());
        }
        if (StringUtils.isNotBlank(equipmentService.getEquipmentType())){
            lqw.eq(EquipmentService::getEquipmentType ,equipmentService.getEquipmentType());
        }
        if (StringUtils.isNotBlank(equipmentService.getEquipment())){
            lqw.eq(EquipmentService::getEquipment ,equipmentService.getEquipment());
        }
        if (StringUtils.isNotBlank(equipmentService.getKeepPeriod())){
            lqw.eq(EquipmentService::getKeepPeriod ,equipmentService.getKeepPeriod());
        }
        if (equipmentService.getIfOpen() != null){
            lqw.eq(EquipmentService::getIfOpen ,equipmentService.getIfOpen());
        }
        List<EquipmentService> list = iEquipmentServiceService.list(lqw);
        List<EquipmentServiceDTO> list2 = new ArrayList<>();
        if(IterUtil.isNotEmpty(list)){
            list.stream().forEach(i->{
                EquipmentServiceDTO equipmentServiceDTO = new EquipmentServiceDTO();
                BeanUtil.copyProperties(i,equipmentServiceDTO);

                if(StrUtil.isNotEmpty(i.getEquipment())){
                    List<Long> equipmentL = Arrays.stream(i.getEquipment().split(","))
                            .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
                    equipmentServiceDTO.setEquipment(equipmentL);
                }
                if(StrUtil.isNotEmpty(i.getEquipmentType())){
                    List<Long> typeL = Arrays.stream(i.getEquipmentType().split(","))
                            .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
                    equipmentServiceDTO.setEquipmentType(typeL);
                }
                list2.add(equipmentServiceDTO);
            });
        }
        return getDataTable(list2);
    }

    @ApiOperation(value = "获取保养项目所有列表",response = EquipmentServiceDTO.class)
    @GetMapping("/getList")
    public List<EquipmentServiceDTO> getList(EquipmentService equipmentService)
    {
        LambdaQueryWrapper<EquipmentService> lqw = new LambdaQueryWrapper<EquipmentService>();
        if (StringUtils.isNotBlank(equipmentService.getItemCode())){
            lqw.eq(EquipmentService::getItemCode ,equipmentService.getItemCode());
        }
        if (StringUtils.isNotBlank(equipmentService.getItemName())){
            lqw.like(EquipmentService::getItemName ,equipmentService.getItemName());
        }
        if (StringUtils.isNotBlank(equipmentService.getKeepDetailed())){
            lqw.eq(EquipmentService::getKeepDetailed ,equipmentService.getKeepDetailed());
        }
        if (StringUtils.isNotBlank(equipmentService.getWorkingHours())){
            lqw.eq(EquipmentService::getWorkingHours ,equipmentService.getWorkingHours());
        }
        if (StringUtils.isNotBlank(equipmentService.getEquipmentType())){
            lqw.eq(EquipmentService::getEquipmentType ,equipmentService.getEquipmentType());
        }
        if (StringUtils.isNotBlank(equipmentService.getEquipment())){
            lqw.eq(EquipmentService::getEquipment ,equipmentService.getEquipment());
        }
        if (StringUtils.isNotBlank(equipmentService.getKeepPeriod())){
            lqw.eq(EquipmentService::getKeepPeriod ,equipmentService.getKeepPeriod());
        }
        if (equipmentService.getIfOpen() != null){
            lqw.eq(EquipmentService::getIfOpen ,equipmentService.getIfOpen());
        }
        List<EquipmentService> list = iEquipmentServiceService.list(lqw);
        List<EquipmentServiceDTO> list2 = new ArrayList<>();
        if(IterUtil.isNotEmpty(list)){
            list.stream().forEach(i->{
                EquipmentServiceDTO equipmentServiceDTO = new EquipmentServiceDTO();
                BeanUtil.copyProperties(i,equipmentServiceDTO);

                if(StrUtil.isNotEmpty(i.getEquipment())){
                    List<Long> equipmentL = Arrays.stream(i.getEquipment().split(","))
                            .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
                    equipmentServiceDTO.setEquipment(equipmentL);
                }
                if(StrUtil.isNotEmpty(i.getEquipmentType())){
                    List<Long> typeL = Arrays.stream(i.getEquipmentType().split(","))
                            .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
                    equipmentServiceDTO.setEquipmentType(typeL);
                }
                list2.add(equipmentServiceDTO);
            });
        }
        return list2;
    }

    /**
     * 导出设备保养项目列表
     */
    @Log(title = "设备保养项目" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(EquipmentService equipmentService) {
        LambdaQueryWrapper<EquipmentService> lqw = new LambdaQueryWrapper<EquipmentService>(equipmentService);
        List<EquipmentService> list = iEquipmentServiceService.list(lqw);
        ExcelUtil<EquipmentService> util = new ExcelUtil<EquipmentService>(EquipmentService. class);
        return util.exportExcel(list, "service" );
    }

    /**
     * 获取设备保养项目详细信息
     */
    @ApiOperation(value = "获取设备保养项目详细信息")
    @GetMapping(value = "/{byId}" )
    public AjaxResult getInfo(@PathVariable("byId" ) Long byId) {
        EquipmentService eq = iEquipmentServiceService.getById(byId);

        EquipmentServiceDTO equipmentServiceDTO = new EquipmentServiceDTO();
        BeanUtil.copyProperties(eq,equipmentServiceDTO);

        List<Long> equipmentL = Arrays.stream(eq.getEquipment().split(","))
                .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
        equipmentServiceDTO.setEquipment(equipmentL);

        List<Long> typeL = Arrays.stream(eq.getEquipmentType().split(","))
                .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
        equipmentServiceDTO.setEquipmentType(typeL);


        return AjaxResult.success(equipmentServiceDTO);
    }

    /**
     * 新增设备保养项目
     */
    @ApiOperation(value = "新增设备保养项目")
    @Log(title = "设备保养项目" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentServiceDTO dto) {
        return toAjax(iEquipmentServiceService.insertWithSchedule(dto) ? 1 : 0);
    }

    /**
     * 修改设备保养项目
     */
    @ApiOperation(value = "修改设备保养项目")
    @Log(title = "设备保养项目" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentServiceDTO dto) {
        EquipmentService equipmentService = new EquipmentService();
        BeanUtil.copyProperties(dto,equipmentService);
        String eq = dto.getEquipment().stream().map(String::valueOf).collect(Collectors.joining(","));
        equipmentService.setEquipment(eq);
        String type = dto.getEquipmentType().stream().map(String::valueOf).collect(Collectors.joining(","));
        equipmentService.setEquipmentType(type);

        //这里因为是执行
        equipmentService.setCreateBy(SecurityUtils.getUsername());
        equipmentService.setCreateTime(new Date());
        return toAjax(iEquipmentServiceService.updateById(equipmentService) ? 1 : 0);
    }

    /**
     * 删除设备保养项目
     */
    @ApiOperation(value = "删除设备保养项目")
    @Log(title = "设备保养项目" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{byIds}" )
    public AjaxResult remove(@PathVariable Long[] byIds) {
        int result=iEquipmentServiceService.removeWithSchedule(Arrays.asList(byIds));
        return toAjax(result);
    }
}
